import {_decorator, AnimationClip, Component, SkeletalAnimation, SkeletalAnimationState} from 'cc';
import {GameManager} from "db://assets/scripts/game/GameManager";
import {MonsterController} from "db://assets/scripts/game/role/monster/MonsterController";
import {MonsterAnimationType} from "db://assets/scripts/game/role/monster/MonsterAnimationType";
import {GameState} from "db://assets/scripts/game/GameState";

const {ccclass, property} = _decorator;

/**
 * 怪物动画脚本
 */
@ccclass('MonsterModelController')
export class MonsterModelController extends Component {
    /**
     * 动画播放组件
     */
    @property(SkeletalAnimation)
    public aniComPlayer: SkeletalAnimation;

    /**
     * 当前动画是否正在播放
     */
    public isAniPlaying: Boolean = false;
    /**
     * 小怪控制器
     */
    public scriptMonster: MonsterController;

    /**
     * 动画类型
     */
    private aniType: string = "";
    /**
     * 动画播放状态
     */
    private aniState: SkeletalAnimationState;

    /**
     * 是否正在跑
     */
    public isRunning() {
        return this.aniType === MonsterAnimationType.RUN && this.isAniPlaying;
    }

    /**
     * 是否站立
     */
    public isIdle() {
        return this.aniType === MonsterAnimationType.IDLE && this.isAniPlaying;
    }

    /**
     * 是否正在攻击
     */
    public isAttacking() {
        return (this.aniType === MonsterAnimationType.ATTACK && this.isAniPlaying)
            || (this.aniType === MonsterAnimationType.ATTACK_1 && this.isAniPlaying)
            || (this.aniType === MonsterAnimationType.ATTACK_2 && this.isAniPlaying);
    }

    /**
     * 是否正在受到攻击
     */
    public isHitting() {
        return this.aniType === MonsterAnimationType.HIT && this.isAniPlaying;
    }

    /**
     * attack动画帧事件
     */
    onFrameAttack(isNormalAttack: boolean = true) {
        if (GameManager.getInstance().gameStateProxy.getGameState() === GameState.OVER
            || GameManager.getInstance().gameStateProxy.getGameState() === GameState.PAUSE) {
            return;
        }

        this.scriptMonster?.releaseSkillToPlayer(isNormalAttack);
    }

    /**
     * 播放小怪动画
     *
     * @param {string} aniType 动画类型
     * @param {boolean} [isLoop=false] 是否循环
     * @param {Function} [callback] 回调函数
     * @param {number} [pos] 调用播放动画的位置，方便用于测试
     * @returns
     * @memberof Player
     */
    public playAni(aniType: string, isLoop: boolean = false, callback?: Function, pos?: number) {
        // console.log("monsterAniType", aniType, "curAni", this.aniType, "pos", pos);

        this.aniType = aniType;
        this.aniComPlayer?.play(aniType);
        this.isAniPlaying = true;

        this.aniState = this.aniComPlayer?.getState(aniType) as SkeletalAnimationState;

        if (this.aniState) {
            if (isLoop) {
                this.aniState.wrapMode = AnimationClip.WrapMode.Loop;
            } else {
                this.aniState.wrapMode = AnimationClip.WrapMode.Normal;
            }

            switch (aniType) {
                case MonsterAnimationType.ATTACK:
                    this.aniState.speed = GameManager.getInstance().gameSpeedProxy.getGameSpeed() * GameManager.getInstance().chapter.attackSpeedAddition;
                    break;
                case MonsterAnimationType.ATTACK_1:
                    this.aniState.speed = GameManager.getInstance().gameSpeedProxy.getGameSpeed() * GameManager.getInstance().chapter.attackSpeedAddition * this.scriptMonster.currentAttackSpeed;
                    break;
                case MonsterAnimationType.ATTACK_2:
                    this.aniState.speed = GameManager.getInstance().gameSpeedProxy.getGameSpeed() * GameManager.getInstance().chapter.attackSpeedAddition * this.scriptMonster.currentAttackSpeed;
                    break;
                case MonsterAnimationType.RUN:
                    // this.aniState.speed = GameManager.getGameSpeed * (this.scriptMonster.curMoveSpeed * GameManager.moveSpeedAddition / this.scriptMonster.baseInfo.moveSpeed);
                    // this.aniState.speed = GameManager.getGameSpeed * (this.scriptMonster.curMoveSpeed * GameManager.moveSpeedAddition / this.scriptMonster.baseInfo.moveSpeed);
                    break;
                case MonsterAnimationType.IDLE:
                    this.aniState.speed = GameManager.getInstance().gameSpeedProxy.getGameSpeed();
                    break;
                default:
                    this.aniState.speed = GameManager.getInstance().gameSpeedProxy.getGameSpeed();
                    break;
            }
        }

        if (!isLoop) {
            this.aniComPlayer.once(SkeletalAnimation.EventType.FINISHED, () => {
                this.isAniPlaying = false;
                callback && callback();
            })
        }
    }
}

